﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.SupplierManage
{
    public partial class List : Common.PageBase
    {

        public string ModifyUrl = "Modify.aspx?SUPPLIERID={0}";
        T_SUPPLIER_BLL bll = new T_SUPPLIER_BLL();
        //SqlServerDAL<T_SUPPLIER_Entity> dal = new SqlServerDAL<T_SUPPLIER_Entity>();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            gvList.PageSize = StaticConstant.PageSize;
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                

                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定




        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            #region MyRegion
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLIERID"]))
            {
                sql.Append(" and SUPPLIERID like '%" + Request.QueryString["SUPPLIERID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLIERCODE"]))
            {
                sql.Append(" and SUPPLIERCODE like '%" + Request.QueryString["SUPPLIERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLIERSHORTNAME"]))
            {
                sql.Append(" and SUPPLIERSHORTNAME like '%" + Request.QueryString["SUPPLIERSHORTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLIERNAME"]))
            {
                sql.Append(" and SUPPLIERNAME like '%" + Request.QueryString["SUPPLIERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CORPCODE"]))
            {
                sql.Append(" and CORPCODE like '%" + Request.QueryString["CORPCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BRANDCODE"]))
            {
                sql.Append(" and BRANDCODE like '%" + Request.QueryString["BRANDCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUSLOG"]))
            {
                sql.Append(" and STATUSLOG like '%" + Request.QueryString["STATUSLOG"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FTIMESTAMPBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("FTIMESTAMP", Request.QueryString["FTIMESTAMPBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FTIMESTAMPEnd"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("FTIMESTAMP", Request.QueryString["FTIMESTAMPEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OPMGR"]))
            {
                sql.Append(" and OPMGR like '%" + Request.QueryString["OPMGR"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OPMGRPHONE"]))
            {
                sql.Append(" and OPMGRPHONE like '%" + Request.QueryString["OPMGRPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LINKMAN"]))
            {
                sql.Append(" and LINKMAN like '%" + Request.QueryString["LINKMAN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LINKPHONE"]))
            {
                sql.Append(" and LINKPHONE like '%" + Request.QueryString["LINKPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["WORKPHONE"]))
            {
                sql.Append(" and WORKPHONE like '%" + Request.QueryString["WORKPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LINKMOBILE"]))
            {
                sql.Append(" and LINKMOBILE like '%" + Request.QueryString["LINKMOBILE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FAX"]))
            {
                sql.Append(" and FAX like '%" + Request.QueryString["FAX"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ADDRESS"]))
            {
                sql.Append(" and ADDRESS like '%" + Request.QueryString["ADDRESS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMAIL"]))
            {
                sql.Append(" and EMAIL like '%" + Request.QueryString["EMAIL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LEGALPERSON"]))
            {
                sql.Append(" and LEGALPERSON like '%" + Request.QueryString["LEGALPERSON"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLYMODE"]))
            {
                sql.Append(" and SUPPLYMODE like '%" + Request.QueryString["SUPPLYMODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COMPANYURL"]))
            {
                sql.Append(" and COMPANYURL like '%" + Request.QueryString["COMPANYURL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPORTINGCASE"]))
            {
                sql.Append(" and SUPPORTINGCASE like '%" + Request.QueryString["SUPPORTINGCASE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["INDUSTRYTYPE"]))
            {
                sql.Append(" and INDUSTRYTYPE like '%" + Request.QueryString["INDUSTRYTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BANK"]))
            {
                sql.Append(" and BANK like '%" + Request.QueryString["BANK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ACCOUNTNUMBER"]))
            {
                sql.Append(" and ACCOUNTNUMBER like '%" + Request.QueryString["ACCOUNTNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LINKDEPARTMENTID"]))
            {
                sql.Append(" and LINKDEPARTMENTID = '" + Request.QueryString["LINKDEPARTMENTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LINKDEPARTMENTNAME"]))
            {
                sql.Append(" and LINKDEPARTMENTNAME like '%" + Request.QueryString["LINKDEPARTMENTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LINKZIP"]))
            {
                sql.Append(" and LINKZIP like '%" + Request.QueryString["LINKZIP"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SAFETYLICENSE"]))
            {
                sql.Append(" and SAFETYLICENSE like '%" + Request.QueryString["SAFETYLICENSE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CHANGENOTIFICATION"]))
            {
                sql.Append(" and CHANGENOTIFICATION like '%" + Request.QueryString["CHANGENOTIFICATION"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENCODINGTIMEBegin"]))
            {
                sql.Append(" and ENCODINGTIME >= '" + Request.QueryString["ENCODINGTIMEBegin"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENCODINGTIMEEnd"]))
            {
                sql.Append(" and ENCODINGTIME < '" + Request.QueryString["ENCODINGTIMEEnd"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENCODINGISSUEDTIMEBegin"]))
            {
                sql.Append(" and ENCODINGISSUEDTIME >= '" + Request.QueryString["ENCODINGISSUEDTIMEBegin"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENCODINGISSUEDTIMEEnd"]))
            {
                sql.Append(" and ENCODINGISSUEDTIME < '" + Request.QueryString["ENCODINGISSUEDTIMEEnd"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FACTORYPHOTO"]))
            {
                sql.Append(" and FACTORYPHOTO like '%" + Request.QueryString["FACTORYPHOTO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["QUALITYDOCUMENTS"]))
            {
                sql.Append(" and QUALITYDOCUMENTS like '%" + Request.QueryString["QUALITYDOCUMENTS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CERTIFICATION"]))
            {
                sql.Append(" and CERTIFICATION like '%" + Request.QueryString["CERTIFICATION"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENTERPRISEOVERVIEW"]))
            {
                sql.Append(" and ENTERPRISEOVERVIEW like '%" + Request.QueryString["ENTERPRISEOVERVIEW"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSHIPSTRUCTURE"]))
            {
                sql.Append(" and OWNERSHIPSTRUCTURE like '%" + Request.QueryString["OWNERSHIPSTRUCTURE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["RESPECTIVEDIVISION"]))
            {
                sql.Append(" and RESPECTIVEDIVISION like '%" + Request.QueryString["RESPECTIVEDIVISION"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ZIP"]))
            {
                sql.Append(" and ZIP like '%" + Request.QueryString["ZIP"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COUNTRY"]))
            {
                sql.Append(" and COUNTRY like '%" + Request.QueryString["COUNTRY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PROVINCES"]))
            {
                sql.Append(" and PROVINCES like '%" + Request.QueryString["PROVINCES"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COUNTY"]))
            {
                sql.Append(" and COUNTY like '%" + Request.QueryString["COUNTY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REGISTRATIONTYPE"]))
            {
                sql.Append(" and REGISTRATIONTYPE like '%" + Request.QueryString["REGISTRATIONTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REGISTEREDCAPITAL"]))
            {
                sql.Append(" and REGISTEREDCAPITAL like '%" + Request.QueryString["REGISTEREDCAPITAL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LICENSENUMBER"]))
            {
                sql.Append(" and LICENSENUMBER like '%" + Request.QueryString["LICENSENUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["TESTINGEQUIPMENT"]))
            {
                sql.Append(" and TESTINGEQUIPMENT like '%" + Request.QueryString["TESTINGEQUIPMENT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["QUALITYSYSTEMDOC"]))
            {
                sql.Append(" and QUALITYSYSTEMDOC like '%" + Request.QueryString["QUALITYSYSTEMDOC"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CODECARDNUMBER"]))
            {
                sql.Append(" and CODECARDNUMBER like '%" + Request.QueryString["CODECARDNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CODECARDATTACHMENT"]))
            {
                sql.Append(" and CODECARDATTACHMENT like '%" + Request.QueryString["CODECARDATTACHMENT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLIERRELATIONSHIP"]))
            {
                sql.Append(" and SUPPLIERRELATIONSHIP like '%" + Request.QueryString["SUPPLIERRELATIONSHIP"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID = '" + Request.QueryString["BUSINESSDIVISIONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '%" + Request.QueryString["BUSINESSDIVISIONCODE"] + "%'");
            } 
            #endregion

            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
            }
            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_SUPPLIER_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "SUPPLIERID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_SUPPLIER_Entity entity = new T_SUPPLIER_Entity();
            entity.SUPPLIERID = StringHelper.ToInt((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "STATUS":
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            temp = ((Status)Enum.Parse(typeof(Status), temp)).ToString();
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
